<?php

namespace app\admin\model;

use think\Model;

class Order extends Model
{
    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'order_status_text',
        'pay_status_text',
        'buyer_status_text',
        'ship_status_text',
        'create_time_text',
        'confirm_time_text',
        'pay_time_text',
        'ship_time_text'
    ];
    

    
    public function getOrderStatusList()
    {
        return ['undeclared' => __('Order_status undeclared'),'submitted' => __('Order_status submitted'),'processing' => __('Order_status processing'),'success' => __('Order_status success'),'failure' => __('Order_status failure'),'except' => __('Order_status except')];
    }     

    public function getPayStatusList()
    {
        return ['undeclared' => __('Pay_status undeclared'),'submitted' => __('Pay_status submitted'),'processing' => __('Pay_status processing'),'success' => __('Pay_status success'),'failure' => __('Pay_status failure'),'except' => __('Pay_status except')];
    }     

    public function getBuyerStatusList()
    {
        return ['unchecked' => __('Buyer_status unchecked'),'same' => __('Buyer_status same'),'different' => __('Buyer_status different')];
    }     

    public function getShipStatusList()
    {
        return ['undeclared' => __('Ship_status undeclared'),'submitted' => __('Ship_status submitted'),'processing' => __('Ship_status processing'),'success' => __('Ship_status success'),'failure' => __('Ship_status failure'),'except' => __('Ship_status except')];
    }     


    public function getOrderStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBuyerStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['buyer_status']) ? $data['buyer_status'] : '');
        $list = $this->getBuyerStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShipStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['ship_status']) ? $data['ship_status'] : '');
        $list = $this->getShipStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getConfirmTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['confirm_time']) ? $data['confirm_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShipTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ship_time']) ? $data['ship_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setConfirmTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setShipTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function heyPayRecord()
    {
        return $this->belongsTo('HeyPayRecord', 'order_sn', 'order_sn', [], 'LEFT')->setEagerlyType(0);
    }
}
